<?php
require_once("mysqlUser/mysql.php");
require_once("robotBase.php");

class robot
{
	public static $table = "robotdepot";
	//////////////////////////////////////////////////
	/**
	 * 给玩家一个新的机甲
	 * @param mixed $uid 玩家id
	 * @param mixed $id 机甲id
	 * @param mixed $inc 强化等级
	 */	
	public static function giveToUser($uid,$id,$inc)
	{
		$base = new robotBase($id,$inc);
		$hp = $base->maxhp;
		$dur = $base->maxdur;
		
		$wep_m="";
		$wep_s="";
		
		for($i=0;$i<$base->con_m;$i++)
		{
			$wep_m = $wep_m."0";
			if($i!=$base->con_m-1)$wep_m=$wep_m.",";
		}
		for($i=0;$i<$base->con_s;$i++)
		{
			$wep_s=$wep_s."0";
			if($i!=$base->con_s-1)$wep_s=$wep_s.",";
		}		
		
		$command =  "INSERT INTO ".robot::$table." VALUES (null,$uid,$id,$inc,0,'$wep_m','$wep_s',$hp,$dur)"; 
		mysql::run($command);
	}
	//////////////////////////////////////
	
	private $pid;//机甲序列号
	private $uid;//所属用户
	private $id;//属性编号
	private $inc;//强化等级
	private $equip;//是否是当前装备
	private $wep_m;//主武器接口的装备情况
	private $wep_s;//副武器接口的装备情况
	private $hp;//生命值
	private $dur;//耐久值
	
	private $baseValues;//属性
	//////////////////////////////////////////////////
    public function __set($name, $value) 
    {
		if (isset($this->$name))
        {       
			if($name=='uid' || $name=='inc' || $name=='equip'|| $name=='hp'|| $name=='dur')	
			{
				$command = "update ".robot::$table." set $name=$value where pid=$this->pid ";
				mysql::run($command);
			}elseif($name=='wep_m' || $name=='wep_s')
			{
				$command = "update ".robot::$table." set $name='$value' where pid=$this->pid ";
				mysql::run($command);
			}
         } 
		
		$this->$name = $value;
    }
     
    public function __get($name)
    {
         if( isset( $this->$name ) ) 
         {    
			if($name=='uid' || $name=='inc' || $name=='equip'|| $name=='wep_m'|| $name=='wep_s'|| $name=='hp'|| $name=='dur')	
			{
				$command = "select * from ".robot::$table." where pid=$this->pid";
				$result =  mysql::run($command);
				$vals=mysql_fetch_array($result);
				$this->$name = $vals[$name];	
			}
			
            return $this->$name;
			
         } else {
            trigger_error( $name . ' variables undefined',  E_USER_NOTICE );
         }
    }
	//////////////////////////////////////////////////
	/**
	 * 玩家拥有的机甲
	 * @param mixed $pid 机甲的物品编号
	 */
	public function robot($pid)
	{		
		$this->pid=$pid;
		
		$command = "select * from ".robot::$table." where pid=$pid";
  		$result = mysql::run($command);
		$vals = mysql_fetch_array($result);
		
		$data = array("uid"=>$vals["uid"],"id"=>$vals["id"],"inc"=>$vals["inc"],"equip"=>$vals["equip"],"wep_m"=>$vals["wep_m"],"wep_s"=>$vals["wep_s"],"hp"=>$vals["hp"],"dur"=>$vals["dur"]);  	
		
		$this->uid=$data["uid"];
    	$this->id=$data["id"];
		$this->inc=$data["inc"];
    	$this->equip = $data["equip"];
		$this->wep_m = $data['wep_m'];
		$this->wep_s = $data["wep_s"];
    	$this->hp = $data["hp"];
    	$this->dur = $data["dur"];
    	
		$this->baseValues = new robotBase($this->id,$this->inc);
	}
	
	public function getWep_m()
	{
		$cons = explode(",",$this->wep_m);
		return $cons;
	}
	
	public function getWep_s()
	{
		$cons = explode(",",$this->wep_s);
		return $cons;
	}
	
	/**
	 * 输入主武器挂接点的信息
	 * 将输入的数组转换为数据库中保存的字符串
	 *
	 * @param mixed $data 挂接点的武器pid(数组，数量与机甲主武器挂接点个数相同)
	 */	
	public function setWep_m($data)
	{
		$wep_m = "";
		
		for($i=0;$i<count($data);$i++)
		{
			$pid = $data[$i];
			$wep_m = $wep_m.$pid;
			
			if($i != count($data)-1)$wep_m = $wep_m.",";
		}
		
		$this->__set('wep_m',$wep_m);
	}
	
	/**
	* 输入副武器挂接点的信息
	* 将输入的数组转换为数据库中保存的字符串
	*
	* @param mixed $data 挂接点的武器pid(数组，数量与机甲副武器挂接点个数相同)
	*/	
	public function setWep_s($data)
	{
		$wep_s = "";
		
		for($i=0;$i<count($data);$i++)
		{
			$pid = $data[$i];
			$wep_s = $wep_s.$pid;
			
			if($i != count($data)-1)$wep_s = $wep_s.",";
		}
		
		$this->__set('wep_s',$wep_s);
	}
	
	public function drop()
	{
		$command = "delete from ".robot::$table." where pid=$this->pid";
    	mysql::run($command);
	}
	
}

?>